/**
 * 
 */
package org.shanktech.tshare.binlogsync.process.parser;

import java.util.HashMap;
import java.util.Map;

import org.shanktech.tshare.binlogsync.model.binlog.BinlogData;
import org.shanktech.tshare.binlogsync.process.BinlogdataParser;
import org.shanktech.tshare.binlogsync.util.Constant;
import org.springframework.util.StringUtils;

public class MixedModelParser implements BinlogdataParser {

  @Override
  public Object parser(BinlogData binlogData, ObjectParser objectParser) {
    String sql = binlogData.getSql();
    Map<String, String[]> result = new HashMap<>();
    if (Constant.SqlDML.INSERT.getValue().equalsIgnoreCase(binlogData.getType())) {
      result = getInsertColumnAndValue(sql);
    } else if (Constant.SqlDML.UPDATE.getValue().equalsIgnoreCase(binlogData.getType())) {
      result = getUpdateColumnAndValue(sql);
    } else if (Constant.SqlDML.DELETE.getValue().equalsIgnoreCase(binlogData.getType())) {
      result = getDeleteColumnAndValue(sql);
    }
    return objectParser.parser(result.get("name"), result.get("value"));
  }
  
  /**
   * 解析 insert sql 语句，获取到操作对象的列名与列值
   * @param sql
   * @return
   */
  private Map<String, String[]> getInsertColumnAndValue(final String sql) {
    Map<String, String[]> resultMap = new HashMap<>();
    String columnStr = sql.substring(sql.indexOf("(")+1, sql.indexOf(")")); // 获取第一个()里的内容
    String valueStr = sql.substring(
        sql.indexOf("(", sql.indexOf(")"))+1 , // 获取第二()里的内容 左边的位置
        sql.indexOf(")", sql.indexOf(")")+1)    // 获取第二()里的内容 右边的位置
        );
    
    if (!StringUtils.isEmpty(columnStr) && !StringUtils.isEmpty(valueStr)) {
      String[] columnArray = columnStr.split(",");
      String[] valueArray = valueStr.split(",");
      
      for (int i = 0; i < columnArray.length; i++) {
        columnArray[i] = getobjectPropertiesName(columnArray[i].trim());
      }
      resultMap.put("name", columnArray);
      
      for (int i = 0; i < valueArray.length; i++) {
        valueArray[i] = valueArray[i].trim().replaceAll("'", "");
      }
      resultMap.put("value", valueArray);
    }
    
    return resultMap;
  }
  
  /**
   * 解析 delete sql 语句，获取where条件的值
   * @param sql
   * @return
   */
  private Map<String, String[]> getDeleteColumnAndValue(final String sql) {
    Map<String, String[]> resultMap = new HashMap<>();
    
    return resultMap;
  }
  
  private Map<String, String[]> getUpdateColumnAndValue(final String sql) {
    Map<String, String[]> resultMap = new HashMap<>();
    
    return resultMap;
  }
  
  private String getobjectPropertiesName(String dbColumnName) {
    StringBuilder sb = new StringBuilder();
    dbColumnName = dbColumnName.replaceAll("`", "");
    String[] tempArray = dbColumnName.split("_");
    if (null != tempArray && tempArray.length > 0) {
      sb.append(tempArray[0]);
      for (int i = 1; i < tempArray.length; i++) {
        sb.append(tempArray[i].substring(0, 1).toUpperCase());
        sb.append(tempArray[i].substring(1));
      }
    }
    return sb.toString();
  }

}
